package com.helen.mybatis.proxy;

import com.helen.mybatis.Enum.CommandTypeEnum;
import com.helen.mybatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


/**
 * @author helenlee
 * @date 2020/4/8
 */
public class ProxyMapper implements InvocationHandler {
    private SqlSession sqlSession;
    private Object target;

    public ProxyMapper(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public ProxyMapper(SqlSession sqlSession, Object target) {
        this.sqlSession = sqlSession;
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> declaringClass = method.getDeclaringClass();
        String name = method.getName();
        String statmentId = declaringClass.getName() + "." + name;
        CommandTypeEnum enumByName = CommandTypeEnum.getEnumByName(name);
        switch (enumByName) {
            case SELECTONE:
            case SELECTlIST:
                return sqlSession.selectList(statmentId, args);
            case INSERT:
            case UPDATE:
            case DELETE:
                return sqlSession.update(statmentId, args);
            default:
                throw new Exception("没有匹配上的");

        }
    }
}
